<template>
	<!-- 查询 -->
	<view class="search">
		<!-- 输入框 -->
		<view class="search-header">
			<input @keydown.enter="searchFn" v-model="text" class="inp" type="text" placeholder="搜索面试题" />
			<view @click="searchFn" class="btn">搜索</view>
		</view>
		<!-- 历史搜索 -->
		<view v-if="historys.length != 0">
			<view class="search-history">
				<text class="t" v-for="(kw, j) in historys" :key="j" @click="findHistoryFn(kw)">{{kw}}</text>
			</view>
			<view class="butt" @click="clearHistoryFn">清空历史</view>
		</view>

		<!-- 题目列表 -->
		<topic-item v-if="arr.length != 0" :data="arr"></topic-item>
		<view v-else class="message">
			暂无搜索内容~~
		</view>
	</view>
</template>
<script>
	import TopicItem from '../../components/TopicItem/TopicItem'

	// 选项式开发
	export default {
		components: {
			TopicItem
		},
		data() {
			return {
				arr: [],
				text: "",
				historys: []
			}
		},
		onLoad() {
			let historysArr = uni.getStorageSync("HISTORYS");
			// console.log(historysArr)
			if (historysArr) {
				this.historys = historysArr;
			}
		},
		methods: {
			findHistoryFn(kw) {
				this.text = kw;
				this.searchFn()
			},
			clearHistoryFn() {
				this.historys = [];
			},
			async searchFn() {
				if (this.text == "") {
					uni.showToast({
						title: "输入框不能为空",
						icon: "none"
					})
					return false;
				}
				// 加载提示
				uni.showLoading({
					title: "正在搜索.."
				})

				// 把关键字添加到数组
				let index = this.historys.indexOf(this.text)
				if (index > -1) {
					this.historys.splice(index, 1);
				}
				this.historys.push(this.text);
				uni.setStorageSync('HISTORYS', this.historys);

				let that = this;
				// 调用云函数
				await uniCloud.callFunction({
						name: "searchInterview",
						data: {
							keyword: this.text
						}
					})
					.then(
						resData => {
							// console.log("搜索结果:", resData)
							if (resData.result && resData.result.data) {
								let data = resData.result.data;
								that.arr = data; //把查询的结果记录起来
								// 把搜索到的面试题列表存储在本地
								uni.setStorageSync("INTERVIEW", data);
							}
						}
					)
					.catch(err => {
						console.log(err)
						// 隐藏提示
						uni.hideLoading()
					})

				// 隐藏提示
				uni.hideLoading()
			}
		}
	}
</script>

<style lang="scss">
	@import "../../static/scss/variable.scss";

	.search {
		padding: $padd;
	}

	.butt {
		color: #ccc;
		margin: 0rpx 10rpx;
		font-size: 32rpx;
	}

	.search-history {
		display: flex;
		flex-wrap: wrap;
		align-items: flex-start;

		.t {
			margin: 10rpx;
			color: #666;
			display: block;
			padding: 6rpx 20rpx;
			border: 1px solid #f0f0f0;
			border-radius: 10rpx;
		}
	}

	.search-header {
		width: 100%;
		display: flex;
		align-items: center;
		padding: 10rpx 0px;

		.inp {
			border: 1px solid #ccc;
			height: 80rpx;
			padding-left: 20rpx;
			flex-grow: 1;
		}

		.btn {
			height: 80rpx;
			line-height: 80rpx;
			padding: 0px !important;
			width: 160rpx;
			border: 1px solid $theme-color;
			background-color: $theme-color;
			color: #fff;
			text-align: center;
		}

	}
</style>